/**
 * Project Name:YCApiServer-common
 * File Name:AudioUtil.java
 * Package Name:com.yc.music.common.util
 * Date:2016年5月12日下午2:38:55
 * Copyright (c) 2016 
 *
 */
package com.yc.music.common.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.UnsupportedAudioFileException;

import org.apache.log4j.Logger;

import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;

/**
 * 音频处理工具类
 * ClassName: AudioUtil <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2016年5月12日 下午2:38:55 <br/>
 *
 * @author panguixiang
 * @version 
 */
public class AudioUtil {

	private static final String duration_key = "duration";
	private static Logger logger = Logger.getLogger(AudioUtil.class);
	
	/**
	 * 获得音频文件的时长，秒为单位
	 * getAudioTime:(这里用一句话描述这个方法的作用). <br/> 
	 * @author panguixiang
	 * @param filePaht
	 * @return
	 * @throws IOException 
	 * @throws UnsupportedAudioFileException 
	 * @since JDK 1.7
	 */
	public static int getAudioTime(String filePaht) throws UnsupportedAudioFileException, IOException {
		File file = new File(filePaht);
//		AudioFileFormat baseFileFormat;
//		int duration=0;
//		try {
//			baseFileFormat = new MpegAudioFileReader().getAudioFileFormat(file);
//			Map<?,?> properties = baseFileFormat.properties();
//			long mil = (long) ((long)properties.get(duration_key) / 1000);
//			int miao= (int) ((mil / 1000) % 60);
//			int fen = (int) ((mil / 1000) / 60);
//			duration = (fen*60)+miao;
//		} catch (UnsupportedAudioFileException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		long ms= 0;
		javax.sound.sampled.AudioFileFormat aff = javax.sound.sampled.AudioSystem.getAudioFileFormat(file);
		java.util.Map props = aff.properties();
		if (props.containsKey("duration")) {
			ms= (long) Math.round((((Long) props.get("duration")).longValue()) / 1000);
		}
		return (int)(ms/1000);
	}
	
	/**
	 * 
	 * doAudioTuning:(调用java 命令行执行 音频处理命令). <br/> 
	 *
	 * @author panguixiang
	 * @param exec
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public static void doAudioTuning(String exec) throws Exception{
		Runtime runtime2 = Runtime.getRuntime();
        Process ps =runtime2.exec(exec);
        BufferedReader  br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = br.readLine())!= null) {
          sb.append(line).append("\n"); 
        }
       String result = sb.toString();
       logger.info("调用C++执行返回的结果："+result);
//       System.out.println(result);
	}
	
/*public static void main(String args[]) {
	StringBuffer buffer = new StringBuffer();
	buffer.append("chmod 777 ");
	buffer.append("/home/yinchaojava/mnt/audiomix/mixAudio").append(" ").
	append("/home/yinchaojava/mnt/testd/yuan.mp3 ").
	append(" /home/yinchaojava/mnt/testd/banzhou.mp3 ")
	.append("/home/yinchaojava/mnt/testd/result.mp3");
	
	String exce = buffer.toString();
	System.out.println("==exce==="+exce);
	Runtime runtime2 = Runtime.getRuntime();
	try {
		 runtime2.exec(exce);
		 Process ps =runtime2.exec(exce);
		 BufferedReader  br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
         StringBuffer sb = new StringBuffer();
         String line;
         while ((line = br.readLine())!= null) {
          sb.append(line).append("\n");
        }
        String result = sb.toString();
        System.out.println(result);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	System.out.println("==exce==="+exce);
}*/
	
	public static void main(String[] args) {
		try {
//			System.out.println(getAudioTime("C:/Users/Administrator/Desktop/temp/empty_hot_temp.mp3"));
			System.out.println(getAudioTime("http://audio.yinchao.cn/201601121616031452586563.mp3"));
			
		} catch (UnsupportedAudioFileException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}